package de.fhin.mas.agents.gripper.behaviours;

import jade.core.Agent;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;
import de.fhin.mas.MASEvents;
import de.fhin.mas.agents.gripper.GripperAgent;

public class MoveGrapplerToDestination extends OneShotBehaviour {

	private static final long serialVersionUID = 1515176226694051361L;

	private final GripperAgent gripperAgent;
	private final MoveGripperPutDown moveGripperPutDown;
	private final MoveGripperTakeUp moveGripperTakeUp;
	private final CheckObjectLost checkObjectLost;

	public MoveGrapplerToDestination(Agent superAgent, MoveGrapplerToStandbyPosition toStandby, MoveGrapplerToTransportPosition toTransport) {
		super(superAgent);
		gripperAgent = (GripperAgent) superAgent;
		moveGripperPutDown = new MoveGripperPutDown(gripperAgent, toStandby, toTransport);
		moveGripperTakeUp = new MoveGripperTakeUp(gripperAgent, toStandby, toTransport);
		checkObjectLost = new CheckObjectLost();
	}

	/**
	 * Bewegt den Greifarm an das zu greifende Objekt heran
	 * 
	 * @param operation
	 */
	public void moveGrappler(String operation) {
		// Greifarm in Aufnahmeposition bewegen
		if (operation == "TAKEUP") {
			gripperAgent.printLn("Moving Grappler to take-up-position");
			ACLMessage loadingObjectMsg = new ACLMessage(ACLMessage.INFORM);
			loadingObjectMsg.setConversationId(MASEvents.LOADING_OBJECT.getName());
			((GripperAgent) myAgent).sendToAll(loadingObjectMsg);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			// Versucht den Greifarm in Aufnahmeposition zu bringen. Liefert zufällig den Rückgabewert true oder false
			// gripperAgent.getRandomBoolean()
			if (true) {
				moveGripperTakeUp.action();
			} else {
				gripperAgent.printLn("Object out of reach!");

				ACLMessage objectOutOfReachMsg = new ACLMessage(ACLMessage.REQUEST);
				objectOutOfReachMsg.setConversationId(MASEvents.OBJECT_OUT_OF_REACH.getName());
				((GripperAgent) myAgent).sendToAll(objectOutOfReachMsg);
			}
		}
		// Greifarm in Ablegeposition bewegen
		else if (operation == "PUTDOWN") {
			gripperAgent.printLn("Moving Grappler to put-down-position");
			ACLMessage unloadingObjectMsg = new ACLMessage(ACLMessage.INFORM);
			unloadingObjectMsg.setConversationId(MASEvents.UNLOADING_OBJECT.getName());
			((GripperAgent) myAgent).sendToAll(unloadingObjectMsg);
			checkObjectLost.startstop(false);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			// Versucht den Greifarm in Ablegeposition zu bringen. Liefert zufällig den Rückgabewert true oder false
			// gripperAgent.getRandomBoolean()
			if (true) {
				moveGripperPutDown.action();
			} else {
				gripperAgent.printLn("Target out of reach!");
			}
		}
		// Greifer befindet sich nicht in Transport-/Standbyposition
		else {
			// gripperAgent.printLn("Error: Grappler is not ready to move to take-up-/put-down-position");
		}
	}

	@Override
	public void action() {

	}

	@Override
	public int onEnd() {
		return 0;
	}
}
